package com.fuershiye.woman.monitor.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;

public class DaoSqlUtil {

	public static StringBuffer appendSort(StringBuffer sb, String sort, Boolean asc){

		if(StringUtils.isBlank(sort)){
			return sb.append("ORDER BY sort ASC ");
		}

		if(asc){
			return sb.append("ORDER BY ").append(sort).append(" ASC ");
		}
		
		return sb.append("ORDER BY ").append(sort).append(" DESC ");
    }
	
	/**
	 * @param sb	拼接前的sql语句
	 * @param key	查询的key
	 * @param value	查询的值
	 * @return		拼接后的sql语句
	 */
	public static StringBuffer appendSql(StringBuffer sb, String key, String value){
		if(StringUtils.isNotBlank(value)){
			sb.append("and "+key+" like:"+key+" ");
		}
		return sb;
	}

	public static StringBuffer appendSql(StringBuffer sb, String key, Long value){
		if(value!=null){
			sb.append("and "+key+" =:"+key+" ");
		}
		return sb;
	}
	
	public static StringBuffer appendSql(StringBuffer sb, String key, List<Long> value){
		if(value!=null && value.size()>0 && value.get(0) !=null){
			sb.append("and "+key+" in(:"+key+") ");
		}
		return sb;
	}
    
//	public static StringBuffer appendSqlInt(StringBuffer sb, String key, List<Integer> value){
//		if(value!=null && value.size()>0 && value.get(0) !=null){
//			sb.append("and "+key+" in(:"+key+") ");
//		}
//		return sb;
//	}
	
    public static StringBuffer appendSql(StringBuffer sb, String key, Integer value){
        if(value!=null){
        	sb.append("and "+key+" =:"+key+" ");
        }
    	return sb;
    }

    //取条件相反的结果
	public static StringBuffer appendSqlOpposite(StringBuffer sb, String key, Integer value){
		if(value!=null){
			sb.append("and "+key+" !=:"+key+" ");
		}
		return sb;
	}

    //query
	public static Query appendQuery(Query query, String key, String value){
		if(StringUtils.isNotBlank(value)){
			query.setString(key, "%" + value + "%");
		}
		return query;
	}

    public static Query appendQuery(Query query, String key, Long value){
    	if(value!=null){
    		query.setLong(key, value);
    	}
    	return query;
    }
    
    public static Query appendQuery(Query query, String key, Integer value){
    	if(value!=null){
        	query.setInteger(key, value);
    	}
    	return query;
    }
    
    public static Query appendQuery(Query query, String key, List<Long> value){
    	if(value!=null && value.size()>0 && value.get(0) !=null){
    		query.setParameterList(key, value);
    	}
    	return query;
    }
    
//    public static Query appendQueryInt(Query query, String key, List<Integer> value){
//    	if(value!=null && value.size()>0 && value.get(0) !=null){
//    		query.setParameterList(key, value);
//    	}
//    	return query;
//    }
    
    //----------------整理之后的-------------------
    //list or one
	public static StringBuffer appendSqlListLong(StringBuffer sb, String key, List<Long> value){
		if(value==null||value.size()<1){
			return sb;
		}
		if(value.size()==1 && value.get(0) !=null){
			sb.append("and "+key+" =:"+key+" ");
		}
		if(value.size()>1){
			sb.append("and "+key+" in(:"+key+") ");
		}
		return sb;
	}
    //list or one
	public static StringBuffer appendSqlListInt(StringBuffer sb, String key, List<Integer> value){
		if(value==null||value.size()<1){
			return sb;
		}
		if(value.size()==1 && value.get(0) !=null){
			sb.append("and "+key+" =:"+key+" ");
		}
		if(value.size()>1){
			sb.append("and "+key+" in(:"+key+") ");
		}
		return sb;
	}
	
	//query list or one
    public static Query appendQueryListLong(Query query, String key, List<Long> value){
    	if(value==null||value.size()<1){
    		return query;
    	}
    	if(value.size()==1 && value.get(0) !=null){
        	query.setLong(key, value.get(0));
    	}
    	if(value.size()>1){
    		query.setParameterList(key, value);
    	}
    	return query;
    }
	//query list or one
    public static Query appendQueryListInt(Query query, String key, List<Integer> value){
    	if(value==null||value.size()<1){
    		return query;
    	}
    	if(value.size()==1){
        	query.setInteger(key, value.get(0));
    	}
    	if(value.size()>1){
    		query.setParameterList(key, value);
    	}
    	return query;
    }
    
    public static void main(String[] args) {
    	
    }
    
}